package org.jeecg.modules.comment.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.article.mapper.ArticleMapper;
import org.jeecg.modules.comment.entity.PhilComment;
import org.jeecg.modules.comment.mapper.PhilCommentMapper;
import org.jeecg.modules.comment.service.IPhilCommentService;
import org.jeecg.modules.comment.vo.CommentPublish;
import org.jeecg.modules.knowledge.mapper.PhilKnowledgeMapper;
import org.jeecg.modules.knowledgecomment.mapper.KnowledgeCommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 评论
 * @Author: jeecg-boot
 * @Date:   2020-11-12
 * @Version: V1.0
 */
@Service
public class PhilCommentServiceImpl extends ServiceImpl<PhilCommentMapper, PhilComment> implements IPhilCommentService {
    @Autowired
    private PhilCommentMapper philCommentMapper;
    @Autowired
    private PhilKnowledgeMapper philKnowledgeMapper;
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private KnowledgeCommentMapper knowledgeCommentMapper;

    @Override
    public Page<CommentPublish> queryListPublishPage(Page<CommentPublish> pageList, String articleId) {
        return pageList.setRecords(philCommentMapper.queryPublishListPage(pageList,articleId));
    }

    @Override
    public Page<CommentPublish> queryListPageById(Page<CommentPublish> pageList, String openId) {
//        // 获取文章的评论
//        List<CommentPublish> list = philCommentMapper.queryListPageByOpenId(openId);
//        // 获取知识的评论
//        List<CommentPublish> knowledgeList = knowledgeCommentMapper.queryListPageByOpenId2(openId);
//        // 合并，并且排序整理
//        List<CommentPublish> commentPublishes = new ArrayList<>();
//        list.forEach(item->{
//            item.setType(1);
//            commentPublishes.add(item);
//        });
//        knowledgeList.forEach(item->{
//            item.setType(0);
//            commentPublishes.add(item);
//        });
//        List<CommentPublish> lis = commentPublishes.stream().sorted(Comparator
//                .comparing(CommentPublish::getCreateTime).reversed()).collect(Collectors.toList());
//        // 返回
//        return pageList.setRecords(lis);

        // 获取用户的所有评论，知识的和文章的通过type区分，0知识1文章
        List<CommentPublish> list = philCommentMapper.queryListPageByOpenId(pageList,openId);
        list.forEach(item->{
            if (item.getType()==0) {
                item.setArticleTitle(philCommentMapper.getTitle(item.getArticleId()));
            }
        });
        return pageList.setRecords(list);
    }

}
